
#include <avr/io.h>
#include <avr/pgmspace.h>

#include <stdint.h>
#include "pgmspacehlp.h"

#include "ADC.h"
#include "LCD_functions.h"
#include <string.h>
#include <stdlib.h>
#include "dataflash.h"

#define Log 1
#define NoLog 0
#define FALSE 0
#define TRUE (!FALSE)

 					
int pageaddress=0;
int bufferaddress=0;

void * datasend (char *tem_send, int buffy)
{
                                  
	   
    if(buffy>=252)									        //if internaladress bigger than 252, will change the page (256 bytes per buffer)/(9bytes of string)=252 bytes per buffer as integer
     {pageaddress++; buffy=0; }							    //counter of the internaladress restart from zero

	if(pageaddress>2074)   								    //change of page
     {pageaddress=0;}
        			
	DF_SPI_init();						    			    //Initialization of SPI
    Read_DF_status(); 	                       			    //Initialization of Dataflash 
    

	Page_To_Buffer (1,pageaddress);	
	Buffer_Write_Str (1,buffy,5,tem_send);			       //Copies one or more bytes to one of the dataflash
 	Buffer_To_Page (1,pageaddress);						   //Transfers a page from dataflash SRAM buffer to flash

															
    DF_CS_inactive;	                                   //End of Dataflash communication

   return 0;
}


char * dataretrieve (char *streingread, int pageintadr)
{
      
    	   
    if(pageintadr>=252)									        //if internaladress bigger than 252, will change the page (256 bytes per buffer)/(9bytes of string)=252 bytes per buffer as integer
     {pageaddress++; pageintadr=0; }							    //counter of the internaladress restart from zero

	if(pageintadr>2074)   								    //change of page
     {pageaddress=0;}
        			

	DF_SPI_init();										//Initialization of SPI
    Read_DF_status(); 									//Initialization of Dataflash 
    
	Page_To_Buffer (1,pageaddress);							//Transfers a page from flash to dataflash SRAM buffer
    Buffer_Read_Str(1,pageintadr,5,streingread);				//Reads one or more bytes from one of the dataflash
        

	DF_CS_inactive;										//End of Dataflash communication

   return *streingread;

}
	

